﻿using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;

namespace MhAdmin.Core.Utils;

/// <summary>
/// 本地邮件工具类
/// </summary>
public static class LocalEmailUtils
{
    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="sender">发送人</param>
    /// <param name="receiver">收件人</param>
    /// <param name="subject">主题</param>
    /// <param name="body">正文</param>
    public static async Task SendTxtEmail(MailSender sender, List<MailboxAddress> receiver, string subject, string body)
    {
        await SendEmail(sender, receiver, subject, body, false);
    }

    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="sender">发送人</param>
    /// <param name="receiver">收件人</param>
    /// <param name="subject">主题</param>
    /// <param name="body">正文</param>
    public static async Task SendHtmlEmail(MailSender sender, List<MailboxAddress> receiver, string subject, string body)
    {
        await SendEmail(sender, receiver, subject, body, true);
    }

    /// <summary>
    /// 发送邮件
    /// </summary>
    /// <param name="sender">发送人</param>
    /// <param name="receiver">收件人</param>
    /// <param name="subject">主题</param>
    /// <param name="body">正文</param>
    /// <param name="html">是否为Html格式</param>
    private static async Task SendEmail(MailSender sender, List<MailboxAddress> receiver, string subject, string body, bool html = false)
    {
        try
        {
            // 创建邮件消息
            MimeMessage message = new()
            {
                Subject = subject,
                Body = new TextPart(html ? "html" : "plain") { Text = body }
            };

            // 添加发件人
            message.From.Add(new MailboxAddress(sender.Address, sender.Address));

            // 添加收件人
            foreach (var mailAddress in receiver)
            {
                message.To.Add(mailAddress);
            }

            // 设置SMTP服务器地址和端口
            using SmtpClient smtp = new();

            // 设置发件人的凭据
            smtp.Connect(sender.SMTPServer, sender.Port, SecureSocketOptions.Auto);
            smtp.Authenticate(sender.Address, sender.SecretKey);

            // 发送邮件
            await smtp.SendAsync(message);

            // 释放资源
            smtp.Disconnect(true);
        }
        catch (Exception ex)
        {
            throw Oops.Bah("邮件发送失败:" + ex.Message);
        }
    }
}

/// <summary>
/// 邮件发送人
/// </summary>
public class MailSender
{
    /// <summary>
    /// 邮件发送人
    /// </summary>
    /// <param name="address">邮件地址</param>
    /// <param name="password">邮件密钥</param>
    public MailSender(string address, string password)
    {
        Address = address;
        SecretKey = password;
        Port = App.GetConfig<int>("Email:Port");
        SMTPServer = App.GetConfig<string>("Email:Host");
    }

    /// <summary>
    /// SMTP服务器地址
    /// </summary>
    public string SMTPServer { get; set; }

    /// <summary>
    /// 邮件端口
    /// </summary>
    public int Port { get; }

    /// <summary>
    /// 邮件密钥
    /// </summary>
    public string SecretKey { get; }

    /// <summary>
    /// 邮件地址
    /// </summary>
    public string Address { get; }
}
